<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="100px"
    >
      <el-form-item label="上级">
        <!-- <treeselect
          :options="optionsTree"
          v-model="form.fParentId"
          placeholder="请选择上级"
          noOptionsText="暂无数据"
          noChildrenText="暂无数据"
        /> -->
        <el-tree-select
          :props="propsSelect"
          v-model="form.fParentId"
          :data="optionsTree"
          :render-after-expand="false"
        />
      </el-form-item>
      <el-form-item label="数据字典名称" prop="cName">
        <el-input v-model="form.cName" placeholder="数据字典名称" />
      </el-form-item>
      <el-form-item label="数据字典值" prop="cValue">
        <el-input v-model="form.cValue" placeholder="数据字典值" />
      </el-form-item>
      <el-form-item label="排序" prop="fSort">
        <el-input v-model="form.fSort" placeholder="排序" />
      </el-form-item>

      <el-form-item label="备注" prop="cDesc">
        <el-input
          v-model="form.cDesc"
          type="textarea"
          placeholder="请输入内容"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  props: {
    optionsTree: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      mode: "add",
      propsSelect: {
        label: "cName",
        children: "childrenList",
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
        show: "查看",
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        fParentId: null,
      },
      //验证规则
      rules: {
        sort: [{ required: true, message: "请输入排序", trigger: "change" }],
        label: [{ required: true, message: "请输入部门名称" }],
      },
      //所需数据选项
      groups: [],
      groupsProps: {
        value: "id",
        emitPath: false,
        checkStrictly: true,
      },
    };
  },
  mounted() {
    // this.getGroup();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //加载树数据
    async getGroup() {
      var res = await this.$API.system.dept.list.get();
      this.groups = res.data;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          try {
            if (!this.form.fParentId) {
              delete this.form.fParentId;
            }
            this.form.fState = 1;
            await (this.mode === "add"
              ? this.$API.system.dicts.AddConfig.post(this.form)
              : this.$API.system.dicts.UpdateConfig.post(this.form));
            this.isSaveing = false;
            this.$emit("success");
            this.visible = false;
            this.$message.success("操作成功");
          } catch (error) {
            this.visible = false;
            // this.$alert(error, "提示", { type: "error" });
          }
          // var res = await this.$API.demo.post.post(this.form);
          // this.isSaveing = false;
          // if (res.code == 200) {
          //   this.$emit("success", this.form, this.mode);
          //   this.visible = false;
          //   this.$message.success("操作成功");
          // } else {
          //   this.$alert(res.message, "提示", { type: "error" });
          // }
        }
      });
    },
    //表单注入数据
    setData(data) {
      //可以和上面一样单个注入，也可以像下面一样直接合并进去
      Object.assign(this.form, data);
    },
  },
};
</script>

<style></style>
